//
//  AFTools.m
//  网络封装
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "AFTools.h"

@implementation AFTools

#pragma mark - GET请求数据
+(void)getDataByURLString:(NSString *)urlString
            WithDataBlock:(myBlock)dataBlock
{
    NSString *string = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
    [manager GET:string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        dataBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"net" object:nil];
    }];
}

#pragma mark - POST请求数据
+(void)postDataByURLString:(NSString *)urlString
                   BodyDic:(NSDictionary *)bodyDic
             WithDataBlock:(myBlock)dataBlock;
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", nil];
    [manager POST:urlString parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        dataBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"net" object:nil];
    }];
}

#pragma mark - 万能JSON解析
+(void)dataByURLString:(NSString *)urlString
               BodyDic:(NSDictionary *)bodyDic
         WithDataBlock:(myBlock)dataBlock
{
    if (bodyDic != nil) {
        [self postDataByURLString:urlString BodyDic:bodyDic WithDataBlock:^(id data) {
            dataBlock(data);
        }];
    }
    else{
        [self getDataByURLString:urlString WithDataBlock:^(id data) {
            dataBlock(data);
        }];
    }
}

#pragma mark - POST请求数据(NSString)
+(void)postDataByURLString:(NSString *)urlString
                BodyString:(NSString *)bodyString
             WithDataBlock:(myBlock)dataBlock
{
    //=&分隔数据
    NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
    NSArray *array = [bodyString componentsSeparatedByString:@"&"];
    for (NSString *each in array) {
        NSArray *arr = [each componentsSeparatedByString:@"="];
        [bodyDic setValue:arr[1] forKey:arr[0]];
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js", @"application/x-javascript", nil];
    [manager POST:urlString parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        dataBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"net" object:nil];
    }];
}

@end
